Javascript Testing

Frisby API Example

Examples for Frisby.js APIs.

1. Expectations

Help test the HTTP response effectively.

  1. expectStatus( code )
    Test the HTTP status code.

  2. expectHeader( key, content )
    Test if the header has the key-value pair key : content. For example,

    Header :

     'Content-Type' : 'application/json'
    

    can be tested by the matcher

     .expectHeader('Content-Type', 'application/json')
    
  3. expectHeaderContains( key, content )
    Pretty much the same with expectHeader. Except for the content here can be a partial string of the exact header.

    For example, header

     'Content-Type' : 'application/json'
    

    can be tested by the matcher

     .expectHeaderContains('Content-Type', 'json')
    
  4. expectJSON( [path], json )
    Test the JSON body in response. [path] is a optional parameter.

    For example, JSON body

     {
         "key1" : "value1"
     }
    

    can be tested by the matcher

     .expectJSON({
         key1 : "value1"
     })
    
  5. expectJSONTypes( [path], json)
    Test the type of JSON values. [path] is a optional parameter.

    For example, JSON body

     {
         "key1" : "value1"
     }
    

    can be tested by the matcher

     .expectJSONTypes({
         key1 : String
     })
    

    Other types like Array and Number can also be used.

  6. Use [path] parameter in expectJSON and expectJSONTypes
    [path] parameter can be used in many situations.

    First, it can be used as a shortcut if you only want to test a part of a JSON.

    For example, you only want to test the arg key and its value in the following JSON.

     {
         "arg" : {
             "key1" : "value1"
         },
         "other_key1" : "other_value1",
         "other_key2" : "other_value2",
         "other_key3" : "other_value3",
         "other_key4" : "other_value4",
     }
    

    This can then be tested by the matcher

     .expectJSON('arg', {
         key1 : "value1"
     })
    
     or
    
     .expectJSONTypes('arg', {
         key1 : String
     })
    

    Path can also be nested such as arg.key1.


    Another usage for [path] is to test objects in a JSON array.

    To test all objects in an array, just append a * to [path]. For example, a JSON array

     {
         "myArray" : [{
             "key" : "value"
         },
         {
             "key" : "value"
         },
         {
             "key" : "value"
         }]
     }
    

    can be tested with the matcher

     .expectJSON('myArray.*', {
         key : "value"
     })
    
     or
    
     .expectJSONTypes('myArray.*', {
         key : String
     })
    

    To test 1 object in an array, just change * to ?.

    For example, a JSON array

     {
         "myArray" : [{
             "key1" : "value1"
         },
         {
             "key2" : "value2"
         },
         {
             "key3" : "value3"
         }]
     }
    

    can be tested with the matcher

     .expectJSON('myArray.?', {
         key1 : "value1"
     })
    
     or
    
     .expectJSONTypes('myArray.?', {
         key1 : String
     })
    
  7. expectBodyContains( content )
    Test if the response body contains certain string.

    For example, response body

     <root>rootValue</root>
    

    can be tested with the matcher

     .expectBodyContains("rootValue")
    
  8. expectJSONLength( [path], length )
    Test the JSON value's length.

    For example,

     {
         "key1" : "value1"
     }
    

    can be tested with the matcher

     .expectJSONLength('key1', 6)
    

2. Helpers

After() and afterJson() can be used to chain one HTTP request after another. For example, you can make a GET request and make another GET request after getting the response.

  1. after()

     frisby.create('First Test')
         .get('first_url')
         .after(function(err, res, body) {
             // make the second request
             frisby.create('Second Test')
                 .get('second_url')
                 .toss();
         })
         .toss()
    
  2. afterJSON()
    This is useful if the response body is JSON.

     frisby.create('First Test')
         .get('first_url')
         .afterJSON(function(json) {
             // json is the JSON body from the first request
    
             // make the second request        
             frisby.create('Second Test')
                 .get('second_url')
                 .toss();
         })
         .toss()
    

3. Inspectors

inspectJSON() and inspectBody() are used to print JSON body and body in console respectively.

Inspectors are useful for viewing details in the response body (probably for debugging).

4. Send JSON

Attach JSON in POST and PUT requests.

For example,

frisby.create('POST JSON')
    .post('url',
    // second parameter is the json body
    {
        key : "value"
    },
    { json: true })
    .toss();

5. Combine with Jasmine functions

All Jasmine functions can be use with Frisby.js.

For example, Suites and Specs can be used as

describe('Suite', function() {
    it('Spec', function() {
        frisby.create('Frisby')
            .get('url')
            .expectStatus(200)
            .toss();
    });
});

Matchers in Jasmine can also be used.

frisby.create('With Jasmine Matchers')
    .get('url')
    .afterJSON(function(json) {
        // use Jasmine matcher to test response body
        expect(json).not.toBe(null);
    })
    .toss();

The example project can be found here.

To run the tests, execute the command node TestServer/app.js to start the server first.

After the server is running,

Http Server Listening on Port: 8080 ...

execute the command jasmine-node spec to run the tests. The following result should show.

.............{"method":"GET"}
.{ method: 'GET' }
....

Finished in 0.283 seconds
18 tests, 20 assertions, 0 failures, 0 skipped

6. References

Frisby.js API Document

Example Project